package org.example.book.normal.state.order;

import org.example.book.util.RedisCommonProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author jeff
 */
@Component
public class DeprecatedCreateOrder extends DeprecatedOrderState{

    @Autowired
    private RedisCommonProcessor commonProcessor;
    @Autowired
    private DeprecatedPayOrder payOrder;

    @Override
    protected DeprecateOrder createOrder(String orderId, String productId, DeprecatedOrderContext context) {
        DeprecateOrder order = DeprecateOrder.builder()
                .orderId(orderId)
                .productId(productId)
                .state(ORDER_WAIT_PAY)
                .build();
        // 订单存入redis，15分钟有效
        commonProcessor.set(orderId, order, 15 * 60, TimeUnit.SECONDS);
        context.setCurrentState(this.payOrder);
        super.notifyObserver(orderId, ORDER_WAIT_PAY);
        return order;
    }
}
